package com.mark.imagedemo.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * @author Markcc
 * @date 2018/11/29-8:37
 * @email markerccc@163.com
 */
public class FileUploadUtil {

    /**
     * 文件上传
     *
     * @param file
     * @param fileName
     * @param filePath
     * @return
     */
    public static String FileUpload(byte[] file, String fileName, String filePath) throws IOException {
        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath + "/" + fileName);
        out.write(file);
        out.flush();
        out.close();
        return null;
    }

    /**
     * 得到新的文件名
     *
     * @param fileOriginName
     * @return
     */
    public static String getFileName(String fileOriginName) {
        return getUUID() + getSuffix(fileOriginName);
    }

    /**
     * 得到文件后缀名
     *
     * @param fileName
     * @return
     */
    public static String getSuffix(String fileName) {
        return fileName.substring(fileName.lastIndexOf("."));
    }

    /**
     * 生成新的UUID
     * replace替换方法
     *
     * @return 返回新的UUID
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static void main(String[] args) {
        String name = "123.png";
        String suffix = getSuffix(name);
        System.out.println(suffix);
    }
}
